var postData = require("../../../data/post_data.js");
var app = getApp();
Page({
    data: {
        playMuisc: false,
    },
    onLoad: function(option) {
        //每一篇文章的id
        var postId = option.id;

        //如果需要在别的方法使用这个id 必须给赋值到data上
        this.data.currenId = option.id;

        var postDate = postData.post_list[postId];

        //绑定值在页面上获取
        this.setData({
            postDate: postDate
        });
        //判断当前页面是播放状态，并且当前的globlMusicId不为null时当前页面才会播放的状态
        if (app.globlData.globlMusicData && app.globlData.globlMusicId === postId) {
            this.setData({
                playMuisc: true
            })
        }
        //获取同步缓存里面设置的值
        var postCollected = wx.getStorageSync("post_Collected");
        if (postCollected) {
            var p_collected = postCollected[postId];
            //判断当前页面是不是有的缓存值里是不是有id
            if (p_collected) {
                this.setData({
                    collected: p_collected
                })
            }
        } else {

            var postCollected = {};
            postCollected[postId] = false;
            wx.setStorageSync("post_Collected", postCollected); //设置同步缓存值
        }
    },
    postCollecedTap: function(event) {
        var postCollected = wx.getStorageSync("post_Collected");
        var post_id = postCollected[this.data.currenId];
        post_id = !post_id;
        postCollected[this.data.currenId] = post_id;

        //使用wx.showModal收藏或取消收藏该文章
        // this.showModal(postCollected, post_id);

        //使用wx.showToast收藏或取消收藏该文章
        this.showToast(postCollected, post_id);

    },
    showToast: function(postCollected, post_id) {
        wx.setStorageSync("post_Collected", postCollected);
        this.setData({
            collected: post_id
        });
        wx.showToast({
            title: post_id ? "收藏成功" : "取消收藏"
        })
    },
    showModal: function(postCollected, post_id) {
        var that = this
        wx.showModal({
            // title: post_id ? "收藏成功" : "取消收藏",
            content: post_id ? "是否收藏该文章？" : "是否取消收藏改文章？",
            showCancel: "取消",
            confirmText: "确定",
            success: function(res) {
                if (res.confirm) {
                    wx.setStorageSync("post_Collected", postCollected);
                    that.setData({
                        collected: post_id
                    });
                }
            }
        })
    },
    showActionSheet: function() {
        var itemList = [
            "分享到微信",
            "分享到朋友圈",
            "分享到QQ"
        ]
        wx.showActionSheet({
            itemList: itemList,
            itemColor: "#405f80",
            success: function(res) {
                wx.showModal({
                    title: "用户" + itemList[res.tapIndex]
                })
            }
        })
    },
    onTapMusic: function(event) {
        var currenId = this.data.currenId;
        var postsData = postData.post_list[currenId];
        var that = this;
        //判断当前是播放还在暂停的状态
        if (this.data.playMuisc) {
            wx.pauseBackgroundAudio();
            this.setData({
                playMuisc: false
            })
        } else {
            wx.playBackgroundAudio({
                dataUrl: "http://ra01.sycdn.kuwo.cn/resource/n3/32/56/3260586875.mp3",
                title: postsData.music.title,
                coverImgUrl: postsData.music.coverImg,
            })
            this.setData({
                playMuisc: true
            })
        }
        //监听播放状态
        wx.onBackgroundAudioPlay(function() {
            that.setData({
                playMuisc: true
            });
            app.globlData.globlMusicData = true; //当前时播放状态赋值为true
            app.globlData.globlMusicId = that.data.currenId; //当前播放的是哪一段音乐的id
        });
        //监听暂停状态
        wx.onBackgroundAudioPause(function() {
            that.setData({
                playMuisc: false
            });
            app.globlData.globlMusicData = false; //当前是暂停状态
            app.globlData.globlMusicId = null; //当前播放的是哪一段音乐的id，当当前页面的音乐是暂停是 给当前页面的id赋值为null
        });
        wx.onBackgroundAudioStop(function(){
            that.setData({
                playMuisc: false
            });
            app.globlData.globlMusicData = false; //当前是暂停状态
            app.globlData.globlMusicId = null;
        });
    },
})